package Hash;

public class SortLink {
	private Link first;
	
	public SortLink() {
		this.first = null;
	}
	
	public void insert(int i) {
		Link theLink = new Link(i);
		Link previous = null;
		Link current = this.first;
		
		while(current!=null && current.getKey() < i) {
			previous = current;
			current = current.next;
		}
		
		if(previous == null) {
			this.first = theLink;
		}else {
			previous.next = theLink;
			theLink.next = current;
		}
	}
	
	public void delete(int key) {
		Link previous = null;
		Link current = this.first;
		
		while(current != null && key != current.getKey()) {
			previous = current;
			current = current.next;
		}
		
		if(previous == null) {
			this.first = this.first.next;
		}else {
			previous.next = current.next;
		}
	}
	
	public boolean find(int key) {
		Link current = this.first;
		
		while(current != null && key <= current.getKey()) {
			if(current.getKey() == key) {
				return true;
			}
			
			current = current.next;
		}
		
		return false;
	}
	
	public void display() {
		System.out.print("List [first] --> [last]");
		
		Link current = this.first;
		
		while(current != null) {
			current.display();
			current = current.next;
		}
	}
}
